﻿
namespace Oddo.Concept.Crm.Common.Models
{
    using System;
    using System.Collections.Generic;
    using Newtonsoft.Json;
    using Oddo.Concept.Crm.Common.Audit;
    using Oddo.Concept.Crm.Common.Interfaces;

    /// <summary>
    /// Business model interface contract
    /// </summary>
    public interface IBusinessModel 
        : IDirty, IAuditable
    {
    }

    /// <summary>
    /// Business model interface contract
    /// </summary>
    /// <typeparam name="TId">Le type de l'id unique</typeparam>
    public interface IBusinessModel<TId> : IBusinessModel
    {
        /// <summary>
        /// Obtient ou définit l'Id unique
        /// </summary>
        TId Id { get; set; }
    }

    /// <summary>
    /// Modèle métier de base
    /// </summary>
    public abstract class BusinessModel<TId> : ModelBase, IBusinessModel<TId>, IEquatable<BusinessModel<TId>>
    {
        /// <summary>
        /// Obtient ou définit l'Id unique de l'instance
        /// </summary>
        [JsonProperty("id")]
        public virtual TId Id { get; set; }

        /// <summary>
        /// Obtient ou determines si l'instance a été modifié
        /// </summary>
        [JsonIgnore]
        public virtual bool IsDirty { get; set; }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public virtual bool Equals(BusinessModel<TId> other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return EqualityComparer<TId>.Default.Equals(Id, other.Id);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The object to compare with the current object. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((BusinessModel<TId>) obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return EqualityComparer<TId>.Default.GetHashCode(Id);
        }
        /// <summary>
        /// Obtient ou définit la date à laquelle l'entité a été crée
        /// </summary>
        [JsonProperty("createdon")]
        public virtual DateTime? CreatedOn { get; set; }
        /// <summary>
        /// Obtient ou définit la derniere date à laquelle l'entité a été mis a jour
        /// </summary>
        [JsonProperty("updatedon")]
        public virtual DateTime? UpdatedOn { get; set; }
        /// <summary>
        /// Obtient ou définit le nom de l'utilisateur qui a créer l'entité
        /// </summary>
        [JsonProperty("createdby")]
        public virtual string CreatedBy { get; set; }
        /// <summary>
        /// Obtient ou définit le nom du dernier utilisateur qui a mis a jour l'entité
        /// </summary>
        [JsonProperty("updatedby")]
        public virtual string UpdatedBy { get; set; }
        /// <summary>
        /// Obtient ou définit une chaine de caractère representant l'entité
        /// </summary>
        [JsonProperty("audit")]
        public virtual string AuditString { get; set; }
    }
}